package scene.grid
{
	import actor.Tower;
	import actor.attribute.TowerAttribute;
	
	import capricornus.core.StageManager;
	import capricornus.io.KeyboardManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import scene.Scene;

	/**
	 * 处理塔的开始拖动和放置等问题 
	 * @author kael
	 * 
	 */	
	public class SceneTowerHandleController
	{
		private var _scene:Scene;
		private var _gridController:SceneGridController;
		
		private var _towerID:int;
		private var _currentTower:Tower;
		private var _towerBase:Sprite;
		private var _useGrid:int;
		
		private var _greenGrid:BitmapData;
		private var _redGrid:BitmapData;
		
		private var _isAdd:Boolean = false;
		private var _canPut:Boolean = false;
		
		public function SceneTowerHandleController( scene:Scene )
		{
			_scene = scene;
			init();
		}
		
		private function init():void
		{
			_gridController = _scene.gridController;
			_towerBase = new Sprite();
			_towerBase.alpha = 0.5;
			_towerBase.mouseChildren = false;
			_towerBase.mouseEnabled = false;
			
			_greenGrid = new BitmapData( _gridController.GRID_WIDTH, _gridController.GRID_WIDTH, true, 0xff00ff00 );
			_redGrid = new BitmapData( _gridController.GRID_WIDTH, _gridController.GRID_WIDTH, true, 0xffff0000 );
			
			StageManager.stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true );
		}
		
		private function onKeyDown( e:KeyboardEvent ):void
		{
			if( e.keyCode==Keyboard.ESCAPE )
			{
				stop();
			}
		}
		
		private function clearBase():void
		{
			while( _towerBase.numChildren > 0 )
			{
				var bm:Bitmap = _towerBase.removeChildAt(0) as Bitmap;
				bm.bitmapData = null;
			}
		}
		
		public function startPutDownTowerByID( id:int ):void
		{
			_towerID = id;
			
			if( _currentTower != null ) 
			{
				_currentTower.destroy()
				_currentTower = null;
			}
			clearBase();
			
			_currentTower = Tower.createTowerByID( id );
			_currentTower.alpha = 0.8;
			_currentTower.stopFire();
			_currentTower.interactiveEnabled = false;
			_useGrid = (_currentTower.attribute as TowerAttribute).useGrid;
			for( var i:int=0; i<_useGrid; i++ )
			{
				for( var j:int=0; j<_useGrid; j++ )
				{
					var bm:Bitmap = new Bitmap();
					bm.bitmapData = _redGrid;
					bm.x = i * _gridController.GRID_WIDTH;
					bm.y = j * _gridController.GRID_WIDTH;
					_towerBase.addChild( bm );
				}
			}
			_currentTower.setLocation( _useGrid * _gridController.GRID_WIDTH/2, _useGrid * _gridController.GRID_WIDTH/2, false );
			_towerBase.addChild( _currentTower );
			
			if( !_isAdd )
			{
				_isAdd = true;
				_scene.backLayer.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove, false, 0, true );
				_scene.backLayer.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true );
			}
			_scene.testPutLayer.addChild( _towerBase );
			_canPut = false;
		}
		
		public function stop():void
		{
			if( _isAdd )
			{
				_isAdd = false;
				_scene.backLayer.removeEventListener( MouseEvent.MOUSE_MOVE, onMouseMove );
				_scene.backLayer.removeEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
				
				if( _towerBase.parent != null )
				{
					_towerBase.parent.removeChild( _towerBase );
				}
			}
			
			if( _currentTower != null ) 
			{
				_currentTower.destroy()
				_currentTower = null;
			}
		}
		
		private function onMouseMove( e:MouseEvent ):void
		{
			var x:int = e.localX - _useGrid * _gridController.GRID_WIDTH /2;
			var y:int = e.localY - _useGrid * _gridController.GRID_WIDTH /2;
			var m:int = int(x / _gridController.GRID_WIDTH);
			var n:int = int(y / _gridController.GRID_WIDTH);
			
			x = m * _gridController.GRID_WIDTH;
			y = n * _gridController.GRID_WIDTH;
			
			_towerBase.x = x;
			_towerBase.y = y;
			var k:int = 0;
			
			_canPut = true;
			for( var i:int=0; i<_useGrid; i++, m++ )
			{
				for( var j:int=0; j<_useGrid; j++ )
				{
					var bm:Bitmap = _towerBase.getChildAt( k ) as Bitmap;
					k++;
					var isBlock:Boolean = _gridController.isTowerBlock( m, n+j );
					if( isBlock ) _canPut = false;
					bm.bitmapData = isBlock?_redGrid:_greenGrid;
				}
			}
		}
		
		private function onMouseDown( e:MouseEvent ):void
		{
			onMouseMove( e );
			if( _canPut )
			{
				var i:int = _towerBase.x / _gridController.GRID_WIDTH;
				var j:int = _towerBase.y / _gridController.GRID_WIDTH;
				var t:Tower = Tower.createTowerByID( _towerID );
				t.setLocation( _towerBase.x + _currentTower.x, _towerBase.y + _currentTower.y );
				t.setGridInfo( i, j );
				_scene.addTower( t );
			}
			if( e.shiftKey==false )
			{
				stop();
			}
			
			StageManager.stage.focus = null;
		}
	}
}





















